package com.example.lib_storage.mmkv

import com.google.gson.Gson
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * @author yaotianxue
 * @description ReadWriteProperty：自定义属性委托
 * @date 2025/1/14
 */
class MMKVProperty<T>(private val defValue:T):ReadWriteProperty<MMKVOwner,T> {
    /**
     * 读
     */
    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T {
        return  thisRef.mmkv.run {
            when(defValue){
                is String -> getString(property.name,defValue)
                is Int -> getInt(property.name,defValue)
                is Long -> getLong(property.name,defValue)
                is Float -> getFloat(property.name,defValue)
                is Boolean -> getBoolean(property.name,defValue)
                else -> Unit
            }
        } as T
    }

    /**
     * 写
     */
    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T) {
//        when(value){
//            is String -> thisRef.mmkv.putString(property.name,value)
//            is Int -> thisRef.mmkv.putInt(property.name,value)
//            is Long -> thisRef.mmkv.putLong(property.name,value)
//            is Float -> thisRef.mmkv.putFloat(property.name,value)
//            is Boolean -> thisRef.mmkv.putBoolean(property.name,value)
//            else -> Unit
//        }
        thisRef.mmkv.run { //run let 标准库函数，节省变量
            when(value){
                is String -> putString(property.name,value)
                is Int -> putInt(property.name,value)
                is Long -> putLong(property.name,value)
                is Float -> putFloat(property.name,value)
                is Boolean -> putBoolean(property.name,value)
                else -> Unit
            }
        }
    }
}
//读写的是对象类型
class MMKVNullableProperty<T>(private val clazz:Class<T>?= null, private val defValue:T?=null):ReadWriteProperty<MMKVOwner,T?>{
    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T? {
        return  thisRef.mmkv.run {
            when(defValue){
                is String -> getString(property.name,defValue)
                is Int -> getInt(property.name,defValue)
                is Long -> getLong(property.name,defValue)
                is Float -> getFloat(property.name,defValue)
                is Boolean -> getBoolean(property.name,defValue)
                else -> {//反序列化：字符串----》对象
                    if(clazz == null) return null
                    val json = getString(property.name,"")
                    Gson().fromJson(json,clazz)
                }
            }
        } as T
    }

    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T?) {
        thisRef.mmkv.run {
            when(value){
                is String -> putString(property.name,value)
                is Int -> putInt(property.name,value)
                is Long -> putLong(property.name,value)
                is Float -> putFloat(property.name,value)
                is Boolean -> putBoolean(property.name,value)
                else ->{//序列化：对象转成字符串存储起来
                    putString(property.name,Gson().toJson(value))
                }
            }
        }
    }

}